﻿using CMS.REST;
using System.Diagnostics;

namespace CMS
{
    public static class Permission
    {
        /// <summary>
        /// 导入数据的权限等级
        /// </summary>
        public const int ImportData = 2;

        /// <summary>
        /// 导出数据的权限
        /// </summary>
        public const int ExportData = 10;

        /// <summary>
        /// 进行值班安排所需的权限
        /// </summary>
        public const int DutyArrange = 2;

        /// <summary>
        /// 进行排班所需的权限
        /// </summary>
        public const int CourseArrange = 2;

        /// <summary>
        /// 编辑课程信息的权限(此值会影响菜单是否打开)
        /// </summary>
        public const int EditCourseRoom = 1;

        /// <summary>
        /// 课室资源权限
        /// </summary>
        public static readonly RESTPermission Room = new RESTPermission(10, 1, -1);

        /// <summary>
        /// 学期资源权限
        /// </summary>
        public static readonly RESTPermission Term = new RESTPermission(10, 1, -1);

        /// <summary>
        /// 用户管理权限
        /// </summary>
        public static readonly RESTPermission User = new RESTPermission(0, 0, 0);

        /// <summary>
        /// 换课调整权限
        /// </summary>
        public static readonly RESTPermission CourseAdjust = new RESTPermission(1, 1, 1);

        /// <summary>
        /// 设置默认学期权限
        /// </summary>
        public static readonly int SetDefaultTerm = 0;


        /// <summary>
        /// 助理资源管理权限
        /// </summary>
        public static readonly RESTPermission Assistant = new RESTPermission(10, 2, 2);

        /// <summary>
        /// 建筑物
        /// </summary>
        public static readonly RESTPermission Building = new RESTPermission(0, -1, 0);

        /// <summary>
        /// 课程信息权限
        /// </summary>
        public static readonly RESTPermission CourseRoom = new RESTPermission(10, 1, 1);



        /// <summary>
        /// 当前用户的权限值(仅供参考,以服务器配置为准)
        /// </summary>
        public static int UserPermission { get; private set; } = 10;

        /// <summary>
        /// 检查当前是否有权限进行制定的操作
        /// </summary>
        /// <param name="targetPermission">值为系统配置的权限值(数字0-10,0最高权限)</param>
        /// <returns></returns>
        public static bool CheckPermission(int targetPermission)
        {
            return targetPermission >= UserPermission;
        }

        /// <summary>
        /// 刷新当前权限值,改值仅供参考
        /// </summary>
        /// <returns></returns>
        public static bool RefreshPermission()
        {
            RESTResponse response = RESTClient.HttpGetManaged("/apis/v1/permission");
            if (response.IsSuccess)
            {
                UserPermission = response.Data[0].ToObject<int>();
                Debug.WriteLine("用户的权限等级为:" + UserPermission,"INFO");
                return true;
            }
            else
            {
                UserPermission = 10;
                return false;
            }
        }
    }
}
